-- EastShine
-- Created by chengb Dec/31/2015
-- 机械祭坛格子

-- 初始化
function initMachineAltar(self)
    self.item:removeAllChildren();

    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 判断能否拾取
            if DungeonBossM.isInBossLayer() and not DungeonBossM.canPickUpTarget() then
                alert(getLocStr("pick_up_instance_target_fail"));
                return;
            end

            local function okFunc(classId)
                -- 执行提交
                DungeonActionM.go("machine_altar", self.gridData:getPos(), classId);
            end

            -- 打开建筑提交界面
            self:openBuildingSubmit(okFunc, EQUIP_OPEN_TYPE_SUBMIT_ALL);
        end
    end

    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);
    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);

    if self.gridData.need_prison == 1 then
        -- 初始时需要监狱图标的遮罩
        local prisonIcon = getLevelItemIconPath("prison");
        local prisonImg  = ccui.ImageView:create(prisonIcon);
        prisonImg:setName("prison_icon");

        self.item:addChild(prisonImg);
    end
end

-- 机械祭坛提交事件回调
function onMachineAltarSubmit(self, classId, bonus, isSucc)
    local name = ItemM.query(classId, "name");
    if not isSucc then
        -- 失败了

        -- 播放建筑强光（较弱）
        playBuildingStrongLight(self, true);

        -- 弹字提示
        local msg = string.format(getLocStr("machine_altar_submit_fail"), name);
        local alertList = splitString(msg, "\n");
        splitAlert(alertList);
    else
        -- 成功了

        -- 播放建筑强光
        playBuildingStrongLight(self);

        local msg = string.format(getLocStr("machine_altar_submit_succ"), name);
        local alertList = splitString(msg, "\n");
        splitAlert(alertList);

        if type(bonus) == "table" and #bonus > 0 then
            local bonusInfo = bonus;

            -- 拆分奖励,播放获得奖励的UI效果
            local splBonus = DungeonEggM.splitBonus(bonusInfo);

            -- 因为按策划的需求，飞入的奖励可能是按批次的（比如：钻石奖励30个，那么每批5个，共6批飞入）
            -- 由于显示的当前数量在逻辑层一次就处理好了，由于显示要求（数量逐渐递增）
            -- 因此在这里对当前拥有的数量做一个本地缓存
            local curNum = DungeonEggM.getTotleNumByType(bonusInfo) - splBonus[1][3] * table.getn(splBonus);
            for i, c in pairs(splBonus) do
                if #c > 0 then
                    performWithDelay(self, function ()
                        curNum = curNum + c[3];
                        self:onBuildingBonus({["bonus"] = c}, nil, curNum);
                    end, 0.5 * (i - 1));
                end
            end
        end
    end
end

